//
//  CSWNewFeatureController.m
//  网易彩票
//
//  Created by MAC on 16/6/17.
//  Copyright © 2016年 MAC. All rights reserved.
//

#import "CSWNewFeatureController.h"

#import "CSWCollectionViewCell.h"

#import "UIView+Frame.h"

@interface CSWNewFeatureController () <UIScrollViewDelegate>

@property (nonatomic, assign) CGFloat lastoffsetX;

@property (nonatomic, weak) UIImageView *guideView;
@property (nonatomic, weak) UIImageView *guideLargeT;
@property (nonatomic, weak) UIImageView *guideLargeS;

@end

@implementation CSWNewFeatureController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init{
    // 创建流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置cell尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    // 设置最小行间距 10
    layout.minimumLineSpacing = 0;//左右之间的间距
    // 设置每个cell之间间距
    layout.minimumInteritemSpacing = 0;
    // 设置每一组的间距，距离顶部的距离
//    layout.sectionInset = UIEdgeInsetsMake(100, 0, 0, 0);
    // 设置滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return  [self initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Register cell classes
    [self setCollectionView];
    
    [self setUpAllChildView];
}

#pragma mark-- UIScrollerView代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 获取偏移量差
    CGFloat offsetGap = offsetX - _lastoffsetX;
    
    
    // guideView
    _guideView.x += 2 * offsetGap;
    
    // largeText
    _guideLargeT.x += 2 * offsetGap;
    
    // smallText
    _guideLargeS.x += 2 * offsetGap;
    
    // 切换图片
    int page = offsetX / self.view.width + 1;
    
    _guideView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide%d",page]];
    
    _guideLargeT.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideLargeText%d",page]];
    
    _guideLargeS.image = [UIImage imageNamed:[NSString stringWithFormat:@"guideSmallText%d",page]];
    
    _lastoffsetX = offsetX;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        _guideView.x -= offsetGap;
        
        _guideLargeT.x -= offsetGap;
        
        _guideLargeS.x -= offsetGap;
        
    }];
}

- (void)setCollectionView{
    [self.collectionView registerClass:[CSWCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.bounces = NO;
}

// 添加所有的子控件
- (void)setUpAllChildView
{
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
    // guide:足球
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    guide.center = CGPointMake(screenW * 0.5, screenH * 0.5);
    [self.collectionView addSubview:guide];
    _guideView = guide;
    
    // 线
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x -= 200;
    [self.collectionView addSubview:guideLine];
    
    // 大文字
    UIImageView *guideLargeT = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    guideLargeT.center = CGPointMake(screenW * 0.5, screenH * 0.8);
    _guideLargeT = guideLargeT;
    [self.collectionView addSubview:guideLargeT];
    
    // 小文字
    UIImageView *guideLargeS = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    guideLargeS.center = CGPointMake(screenW * 0.5, screenH * 0.9);
    _guideLargeS = guideLargeS;
    [self.collectionView addSubview:guideLargeS];
}

#pragma mark --  数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (CSWCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CSWCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.row + 1];
    
    cell.image = [UIImage imageNamed:imageName];
    [cell setIndexPath:indexPath count:4];
    //cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}

@end
